﻿using FastIOC.Definition;
using FastIOC.Exception;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastIOC.Entity
{
    public class RegisterEntity
    {
        public Type RegistType { set; get; }
        public Type RealType { set; get; }
        public String Name { set; get; }
        public bool IsEnableIntercept { set; get; }
        public bool IsInterceptAllMethod { set; get; }
        public Type InterceptType { set; get; }
        public Object EntityInstance { set; get; }

        public RegisterEntity(Type RegistType)
        {
            this.RegistType = RegistType;
            RealType = RegistType;
            Name = RegistType.Name;
            IsEnableIntercept = false;
            IsInterceptAllMethod = false;
        }

        public RegisterEntity As<T>()
        {
            RealType = typeof(T);
            Name = RealType.Name;
            return this;
        }

        public RegisterEntity Named<T>(string Name)
        {
            RealType = typeof(T);
            this.Name = Name;
            return this;
        }


        public RegisterEntity AsImplementedInterfaces(string Name = "")
        {
            Type[] BaseinterfaceList = RegistType.GetInterfaces();
            if (BaseinterfaceList.Length > 0)
            {
                RealType = BaseinterfaceList[0];
                if (string.IsNullOrEmpty(Name))
                {
                    this.Name = RealType.Name;
                }
                else
                {
                    this.Name = Name;

                }
              
            }
            else
            {
                throw new NoBaseInterfaceException(string.Format("注册类{0}没有实现的接口",RegistType.FullName));
            }
            return this;
        }

        public RegisterEntity EnableIntercept()
        {
            IsEnableIntercept = true;
            IsInterceptAllMethod = true;
            return this;
        }

        public RegisterEntity InterceptedBy<T>() where T : IFastIntercept
        {
            InterceptType = typeof(T);
            return this;
        }

        public RegisterEntity UseAttributeIntercept()
        {
            IsInterceptAllMethod = false;
            return this;
        }
    }
}
